﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace Coding4Fun.Lockscreen.Core.Storage
{
    public static class LocalStorageHelper
    {
        public async static Task<bool> WriteData(string folderName, string fileName, byte[] content)
        {
            try
            {
                IStorageFolder rootFolder = ApplicationData.Current.LocalFolder;

                if (folderName != string.Empty)
                {
                    rootFolder = await rootFolder.CreateFolderAsync(folderName,
                        CreationCollisionOption.OpenIfExists);
                }

                IStorageFile file = await rootFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
                using (var s = await file.OpenStreamForWriteAsync())
                {
                    s.Write(content, 0, content.Length);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        public static async Task<bool> ClearFolder(string folderName)
        {
            try
            {
                StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName, CreationCollisionOption.ReplaceExisting);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static async Task<string> ReadData(string fileName)
        {
            byte[] data;

            StorageFolder folder = ApplicationData.Current.LocalFolder;

            StorageFile file = await folder.GetFileAsync(fileName);

            using (Stream s = await file.OpenStreamForReadAsync())
            {
                data = new byte[s.Length];
                await s.ReadAsync(data, 0, (int)s.Length);
            }

            return Encoding.UTF8.GetString(data, 0, data.Length);
        }

        public static void CycleThroughImages()
        {
            List<string> images = Coding4Fun.Toolkit.Storage.Serialize.Open<List<string>>("imagestack.xml");

            if (images != null)
            {
                string tempImage = images.First();
                Uri currentImageUri = new Uri("ms-appdata:///Local/CurrentSet/" + tempImage, UriKind.Absolute);

                try
                {
                    Windows.Phone.System.UserProfile.LockScreen.SetImageUri(currentImageUri);

                    images.Remove(tempImage);
                    images.Add(tempImage);

                    Coding4Fun.Toolkit.Storage.Serialize.Save<List<string>>("imagestack.xml", images);
                }
                catch
                {
                    Debug.WriteLine("Failed to assign image to wallpaper.");
                }
            }
        }
    }
}
